"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

/*
 * Copyright (C) 2016 Bilibili. All Rights Reserved.
 *
 * @author zheng qian <xqq@xqq.im>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// Utility class to calculate realtime network I/O speed
var SpeedSampler = /*#__PURE__*/function () {
  function SpeedSampler() {
    (0, _classCallCheck2.default)(this, SpeedSampler);
    // milliseconds
    this._firstCheckpoint = 0;
    this._lastCheckpoint = 0;
    this._intervalBytes = 0;
    this._totalBytes = 0;
    this._lastSecondBytes = 0; // compatibility detection

    if (typeof self != 'undefined' && self.performance && self.performance.now) {
      this._now = self.performance.now.bind(self.performance);
    } else {
      this._now = Date.now;
    }
  }

  (0, _createClass2.default)(SpeedSampler, [{
    key: "reset",
    value: function reset() {
      this._firstCheckpoint = this._lastCheckpoint = 0;
      this._totalBytes = this._intervalBytes = 0;
      this._lastSecondBytes = 0;
    }
  }, {
    key: "addBytes",
    value: function addBytes(bytes) {
      if (this._firstCheckpoint === 0) {
        this._firstCheckpoint = this._now();
        this._lastCheckpoint = this._firstCheckpoint;
        this._intervalBytes += bytes;
        this._totalBytes += bytes;
      } else if (this._now() - this._lastCheckpoint < 1000) {
        this._intervalBytes += bytes;
        this._totalBytes += bytes;
      } else {
        // duration >= 1000
        this._lastSecondBytes = this._intervalBytes;
        this._intervalBytes = bytes;
        this._totalBytes += bytes;
        this._lastCheckpoint = this._now();
      }
    }
  }, {
    key: "currentKBps",
    get: function get() {
      this.addBytes(0);
      var durationSeconds = (this._now() - this._lastCheckpoint) / 1000;
      if (durationSeconds == 0) durationSeconds = 1;
      return this._intervalBytes / durationSeconds / 1024;
    }
  }, {
    key: "lastSecondKBps",
    get: function get() {
      this.addBytes(0);

      if (this._lastSecondBytes !== 0) {
        return this._lastSecondBytes / 1024;
      } else {
        // lastSecondBytes === 0
        if (this._now() - this._lastCheckpoint >= 500) {
          // if time interval since last checkpoint has exceeded 500ms
          // the speed is nearly accurate
          return this.currentKBps;
        } else {
          // We don't know
          return 0;
        }
      }
    }
  }, {
    key: "averageKBps",
    get: function get() {
      var durationSeconds = (this._now() - this._firstCheckpoint) / 1000;
      return this._totalBytes / durationSeconds / 1024;
    }
  }]);
  return SpeedSampler;
}();

var _default = SpeedSampler;
exports.default = _default;